﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace UdpServer
{
    internal class Program
    {
        private static int remoteReceivePort = 8012;
        static void Main(string[] args)
        {
            // 将字符串ip转化为ip地址
            IPAddress ip = IPAddress.Parse("127.0.0.1");
            // 创建发送套接字，套接字类型为dgram，协议类型为udp
            Socket sendSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            // 不需要建立连接直接发送消息给远程地址
            sendSocket.SendTo(Encoding.UTF8.GetBytes("测试数据"), new IPEndPoint(ip, remoteReceivePort));
            Console.WriteLine("发送测试数据");
            // 停止发送状态
            sendSocket.Shutdown(SocketShutdown.Send);
            // 关闭连接
            sendSocket.Close();
            Console.ReadLine();
        }
    }
}
